//
//  RootTabViewController.m
//  CarTown
//
//  Created by 熊浩 on 15/7/31.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "RootTabViewController.h"
#import "TabItemMode.h"
#import "CZTabBar.h"
#import "Puser.h"
#import "IndexTableViewController.h"
#import "CarTableViewController.h"
#import "WinViewController.h"
#import "MyUIViewController.h"
#import "IndexNavViewController.h"
#import "FindCarTableViewController.h"

@interface RootTabViewController ()<CZTabBarDelegate>
@property (strong,nonatomic) NSMutableArray *array;
@property (strong,nonatomic) NSMutableArray *items;
@property (nonatomic,strong) Puser *user;

@end

@implementation RootTabViewController

-(Puser *)user{
    if (_user==nil) {
        _user=[Puser getUser];
    }
    return _user;
}


-(NSMutableArray *)array{
    if (_array==nil) {
        _array=[NSMutableArray array];
        TabItemMode *indexmode=[[TabItemMode alloc] init];
        indexmode.title=@"主页";
        indexmode.img=@"index_normal";
        indexmode.selimg=@"index_selected";
       
        TabItemMode *carmode=[[TabItemMode alloc] init];
        carmode.title=@"车源";
        carmode.img=@"car_normal";
        carmode.selimg=@"car_selected";
//        TabItemMode *winmode=[[TabItemMode alloc] init];
//        winmode.title=@"抢标";
//        winmode.img=@"win_normal";
//        winmode.selimg=@"win_selected";
        TabItemMode *mymode=[[TabItemMode alloc] init];
        mymode.title=@"我的";
        mymode.img=@"my_normal";
        mymode.selimg=@"my_selected";
         [_array addObject:indexmode];
        [_array addObject:carmode];
//        [_array addObject:winmode];
        [_array addObject:mymode];
    }
    return _array;
}
- (NSMutableArray *)items
{
    if (_items == nil) {
        
        _items = [NSMutableArray array];
        
    }
    return _items;
}
-(void)setUpItems{
    for (int i=0; i<self.viewControllers.count; i++) {
        
        TabItemMode *mode=self.array[i];
        UIViewController *vc=self.viewControllers[i];
        vc.title = mode.title;
        vc.tabBarItem.image = [UIImage imageNamed:mode.img];
        vc.tabBarItem.selectedImage = [UIImage imageNamed:mode.selimg];
        // 保存tabBarItem模型到数组
        [self.items addObject:vc.tabBarItem];
    }
}
-(void)setupvc{

    IndexTableViewController *indexvc=[[IndexTableViewController alloc]init];
    
    IndexNavViewController *index=[[IndexNavViewController alloc]initWithRootViewController:indexvc];
    FindCarTableViewController *fidvc=[FindCarTableViewController findcar];
    fidvc.hidesBottomBarWhenPushed=NO;
    
    IndexNavViewController *car=[[IndexNavViewController alloc]initWithRootViewController:fidvc];
//    WinViewController *winvc=[[WinViewController alloc]init];
    
//    IndexNavViewController *win=[[IndexNavViewController alloc]initWithRootViewController:winvc];
    MyUIViewController *myvc=[[MyUIViewController alloc]init];
    
    IndexNavViewController *my=[[IndexNavViewController alloc]initWithRootViewController:myvc];
    self.viewControllers=@[index,car,my];
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    [self setupvc];
    [self setUpItems];
    [self setUpTabBar];
    
    [XHUserDeflaut setBool:NO forKey:SHOWFIND];
    self.selectedIndex=0;
    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    [defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];
    // Do any additional setup after loading the view.
}
-(void)networkDidReceiveMessage:(NSNotification *)userinfo{
    NSDictionary * userInfo = [userinfo userInfo];
    
    NSDictionary *extras = [userInfo valueForKey:@"extras"];
    
    NSNumber *power=extras[@"power"];
    if (power!=nil) {
        self.user.power=power;
        [Puser save];
    }

}

#pragma mark - 设置tabBar
- (void)setUpTabBar
{
    // 自定义tabBar
    CZTabBar *tabBar = [[CZTabBar alloc] initWithFrame:self.tabBar.bounds];
    tabBar.backgroundColor = [UIColor whiteColor];
    
    // 设置代理
    tabBar.delegate = self;
    
    // 给tabBar传递tabBarItem模型
    tabBar.items = self.items;
    
    // 添加自定义tabBar
    [self.tabBar addSubview:tabBar];
   
    self.cztabBar=tabBar;
    // 移除系统的tabBar
    
   
}



#pragma mark - 当点击tabBar上的按钮调用
- (void)tabBar:(CZTabBar *)tabBar didClickButton:(NSInteger)index
{
   
    self.selectedIndex = index;
//    if (index!=1) {
//        
//        NSMutableDictionary *userinfo=[NSMutableDictionary dictionary];
//        userinfo[@"needupdate"]=@(YES);
//        
//       NSNotification *notice =[[NSNotification alloc] initWithName:NoticeUpdate object:nil userInfo:userinfo];
//        [[NSNotificationCenter defaultCenter] postNotification:notice];
//        [XHUserDeflaut setBool:NO forKey:SHOWFIND];
//        [XHUserDeflaut setBool:NO forKey:ISFROMSEARCH];
//        [XHUserDeflaut setObject:nil forKey:SEARCHCOND];
//        [XHUserDeflaut setObject:nil forKey:SEARCHCONDDetail];
//    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    for (UIView *tabitem in self.tabBar.subviews) {
//        
//        if ([tabitem isKindOfClass:[UIControl class]]) {
//            [tabitem removeFromSuperview];
//        }
//    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
